/*
 * Service class 
 * Created on 6 juin 2013 ( Time 15:03:32 )
 */

package org.demo.employecalendrier.service;

import java.util.List;

import org.demo.employecalendrier.bean.Employecalendrier;
import org.demo.employecalendrier.dao.JpaEmployecalendrierDAO;
import org.telosys.starterkits.strutsjpa.EntityManagerHelper;
import org.telosys.starterkits.strutsjpa.IServices;
import javax.persistence.PersistenceException;

import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

import org.demo.employecalendrier.bean.EmployecalendrierKey;

public class EmployecalendrierServices implements IServices<Employecalendrier, EmployecalendrierKey> {

	protected final Logger LOG = LoggerFactory.getLogger(EmployecalendrierServices.class);

	public Employecalendrier load(final EmployecalendrierKey id) {
		if (LOG.isDebugEnabled()) LOG.debug("load");
		Employecalendrier employecalendrier;
		try {
			EntityManagerHelper.beginTransaction();
			JpaEmployecalendrierDAO employecalendrierDAO = new JpaEmployecalendrierDAO();
			employecalendrier = employecalendrierDAO.findById(id);
			EntityManagerHelper.commitAndCloseEntityManager();
		} catch (PersistenceException ex) {
			LOG.error("Error", ex);
			throw ex;
		} finally {
			if (EntityManagerHelper.isCloseEntityManager() == false) EntityManagerHelper.rollback();
		}
		return employecalendrier ;
	}

	public Employecalendrier save(final Employecalendrier entity) {
		if (LOG.isDebugEnabled()) LOG.debug("save");
		Employecalendrier entityNew;
		try {
			EntityManagerHelper.beginTransaction();
			JpaEmployecalendrierDAO employecalendrierDAO = new JpaEmployecalendrierDAO();
			entityNew = employecalendrierDAO.update(entity);
			EntityManagerHelper.commitAndCloseEntityManager();
		} catch (PersistenceException ex) {
			LOG.error("Error", ex);
			throw ex;
		} finally {
			if (EntityManagerHelper.isCloseEntityManager() == false) EntityManagerHelper.rollback();
		}
		return entityNew;
	}

	public void delete(final EmployecalendrierKey id) {
		if (LOG.isDebugEnabled()) LOG.debug("delete");
		try {
			EntityManagerHelper.beginTransaction();
			JpaEmployecalendrierDAO employecalendrierDAO = new JpaEmployecalendrierDAO();
			employecalendrierDAO.delete(id);
			EntityManagerHelper.commitAndCloseEntityManager();	
		} catch (PersistenceException ex) {
			LOG.error("Error", ex);
			throw ex;
		} finally {
			if (EntityManagerHelper.isCloseEntityManager() == false) EntityManagerHelper.rollback();
		}
	}

	public List<Employecalendrier> search(final Employecalendrier employecalendrier) {
		if (LOG.isDebugEnabled()) LOG.debug("search");
		List<Employecalendrier> liste;
		try {
			EntityManagerHelper.beginTransaction();
			JpaEmployecalendrierDAO employecalendrierDAO = new JpaEmployecalendrierDAO();
			liste = employecalendrierDAO.search(employecalendrier);
			EntityManagerHelper.commitAndCloseEntityManager();
		} catch (PersistenceException ex) {
			LOG.error("Error", ex);
			throw ex;
		} finally {
			if (EntityManagerHelper.isCloseEntityManager() == false) EntityManagerHelper.rollback();
		}
		return liste;
	}

}
